package com.itbour.web.action.stats;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.itbour.com.page.Page;
import com.itbour.common.base.BaseAction;
import com.itbour.common.util.StringUtils;
import com.itbour.dto.com.UserInfoDto;
import com.itbour.dto.stats.UserDocInfoDto;
import com.itbour.dto.tg.TypeInfoDto;
import com.itbour.service.stats.UserDocStatsService;
import com.opensymphony.xwork2.ActionContext;


/**
 *用户模板使用统计Action 
 */
public class GetUserDocStatsAction extends BaseAction{
	/**
	 * 展示stats表画面 
	 */
	private String isCount;
	private String findPubFlg;// 检索条件：发布标记
	private String findDocName;//检索的名称
	private String findTmplType;// 检索条件--分类计算的 
	private String findTmplTypetop;//顶级
	private String findDateStart;// 检索条件[时间范围(起始)
	private String findDateEnd;// 检索条件[时间范围(截止)
	private List<TypeInfoDto> typeComboList; //展示的信息 提纲信息
	private static final long serialVersionUID = 1L;
	private UserDocStatsService userDocStatsService;
	HashMap<String, Object> mapResult = new HashMap<String, Object>();// 检索查询出来的结果
	HashMap<String, Object> userDocMapResult = new HashMap<String, Object>();// 检索查询出来的结果
	private UserDocInfoDto allUserDocInfo;
	private String findUserId;// 查找用户作品
	private String findDesignerName;// 设计师名字 模糊搜索
	
	public String getMapInfoList() throws Exception{
		// 用户信息
		Map<String, Object> session = ActionContext.getContext().getSession();
		UserInfoDto user = (UserInfoDto)session.get("user");
		if(user!=null && user.getUser_id()!=null) {
			HttpServletRequest request = ServletActionContext.getRequest();
			String pageNumber = request.getParameter("page");
			String pageSize = request.getParameter("rows");
			//获取sort的参数排序
			String sort=request.getParameter("sort");
			String order=request.getParameter("order");
			Map<String, Object> param = new HashMap<String, Object>();
			order = (org.apache.commons.lang3.StringUtils.defaultString(order).length()==0)?"desc":order;
			sort = (org.apache.commons.lang3.StringUtils.defaultString(sort).length()==0)?"numbers":sort;
			StringBuffer ordersb = new StringBuffer(); 
			ordersb.append(sort).append(" ").append(order);
			param.put("order_num", ordersb.toString());//添加参数
			if (StringUtils.isNotBlank(findTmplType) && Long.valueOf(findTmplType)>0L) {
				param.put("type_id", findTmplType);
			}
			if (StringUtils.isNotBlank(findTmplTypetop) && Long.valueOf(findTmplTypetop)>0L) {
				
				param.put("top_type", findTmplTypetop);//顶级的父类来查询的条件 
			}
			if (StringUtils.isNotBlank(findDocName)) {//添加
				param.put("design_name", findDocName);
			}
			if (StringUtils.isNotBlank(findDateStart)) {
				param.put("crtDateStart", findDateStart);
			}
			if (StringUtils.isNotBlank(findDateEnd)) {
				param.put("crtDateEnd", findDateEnd);
			}
			if (StringUtils.isNotBlank(findPubFlg)) {
				param.put("publish", findPubFlg);
			}
			// 默认只检索自己的文档([管理员,模版操作员]除外)
			if(!user.isAdmin() && (user.getRole()&32)!=32) {
				param.put("user_id", user.getUser_id());
			} else {
				if (StringUtils.isNotEmpty(findUserId)){
					param.put("user_id", findUserId); // 权限高的可以查看其它用户作品
				} else {
					if (StringUtils.isNotEmpty(findDesignerName)){
						param.put("user_name",findDesignerName);
					}
				}
			}
			//翻页查询
			Page page = userDocStatsService.pageQueryStats(param, Integer.parseInt(pageNumber),
					Integer.parseInt(pageSize));
			HashMap<String, Object> resMap = new HashMap<String, Object>();
			resMap.put("total", page.getTotalCount());
			resMap.put("rows", page.getResult());
			mapResult = resMap;
		} else {
			mapResult.clear();
		}
		return SUCCESS;
	}
	
	// 获取提纲分类
	public String getTypeForCombo() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		String parent = request.getParameter("parent");
		String flg = request.getParameter("blkFlg");
		String topIds = request.getParameter("topIds");// 逗号连接顶级分类
		Map<String, Object> param = new HashMap<String, Object>();
			if(StringUtils.isNotBlank(parent)){
			param.put("parent", parent);
		}
			if(StringUtils.isNotBlank(topIds)){
			param.put("topIds", topIds);
		}
			if(StringUtils.isNotBlank(flg)){
			param.put("flg", flg);
		}
		typeComboList = userDocStatsService.getTypeForCombo(param);
			if(typeComboList != null && typeComboList.size()>0){
			// 添加空白项目
			    if("1".equals(flg)){
				TypeInfoDto blankItem = new TypeInfoDto();
				blankItem.setType_id(0L);
				blankItem.setType_name("[不限]");
				typeComboList.add(0, blankItem);
			}
			// 默认选中第一个
			typeComboList.get(0).setSelected(true);
		}
		return SUCCESS;
	}
	
	/*
	 * 修改应用次数
	 */
	public void editApplyCnt() throws Exception{
		HttpServletRequest request=ServletActionContext.getRequest();
		Map<String, Object> session=ActionContext.getContext().getSession();
		UserInfoDto user=(UserInfoDto)session.get("user");//获取管理员信息
		if(user!=null&&user.getUser_id()!=null){
			String stats_id=request.getParameter("id");
			String applyCnt=request.getParameter("applyCnt");//应用次数
			if(stats_id!=null&&stats_id.length()>0){
				Map<String, Object> parm=new HashMap<String, Object>();
				parm.put("id", Long.valueOf(stats_id));
				parm.put("show_apply_cnt", Long.valueOf(applyCnt));
				parm.put("update_by", user.getUser_id());
				boolean b=userDocStatsService.editShowViewCnt(parm);
				if(b){
					this.writeJSON("ok");
				}else{
					this.writeJSON("no");
				}
			}else{
				this.writeJSON("no");
			}
		}else{
			this.writeJSON("no");
		}
	}
	
	// 批量更新应用次数
	public void batchSetTmplApplyCnt() throws Exception{
		HttpServletRequest request=ServletActionContext.getRequest();
		Map<String, Object> session=ActionContext.getContext().getSession();
		UserInfoDto user=(UserInfoDto)session.get("user");//获取管理员信息
		
		if(user!=null&&user.getUser_id()!=null){
			String top_type=request.getParameter("top_type");
			String type=request.getParameter("type");
			String base=request.getParameter("base");
			String mult=request.getParameter("mult");
			if(type!=null && type.length()>0 && mult!=null&&mult.length()>0){
				Map<String, Object> parm=new HashMap<String, Object>();
				parm.put("update_by", user.getUser_id());
				parm.put("base", Long.valueOf(base));
				parm.put("mult", Long.valueOf(mult));
				parm.put("top_type", Long.valueOf(top_type));
				parm.put("type", Long.valueOf(type));
				boolean b=userDocStatsService.batchSetShowApplyCnt(parm);
				if(b){
					this.writeJSON("ok");
				}else{
					this.writeJSON("no");
				}
			}else{
				this.writeJSON("no");
			}
		}else{
			this.writeJSON("no");
		}
	}
	
	// 新建用户文档统计
	public String getNewUserDocInfoList() throws Exception{
		// 用户信息
		Map<String, Object> session = ActionContext.getContext().getSession();
		UserInfoDto user = (UserInfoDto)session.get("user");
		if(user!=null && user.getUser_id()!=null) {
			HttpServletRequest request = ServletActionContext.getRequest();
			String pageNumber = request.getParameter("page");
			String pageSize = request.getParameter("rows");
			//获取参数
			String grpBy=request.getParameter("grpBy");// day,week,month
			if(StringUtils.isNotBlank(grpBy)) {
				Map<String, Object> param = new HashMap<String, Object>();
				param.put("grpBy", grpBy);
				if (StringUtils.isNotBlank(findDateStart)) {
					param.put("crtDateStart", findDateStart);
				}
				if (StringUtils.isNotBlank(findDateEnd)) {
					param.put("crtDateEnd", findDateEnd);
				}
				//翻页查询
				Page page = userDocStatsService.pageQueryNewUserDoc(param, Integer.parseInt(pageNumber),
						Integer.parseInt(pageSize));
				HashMap<String, Object> resMap = new HashMap<String, Object>();
				resMap.put("total", page.getTotalCount());
				resMap.put("rows", page.getResult());
				userDocMapResult = resMap;
				
			} else {
				userDocMapResult.clear();
			}
			
		} else {
			userDocMapResult.clear();
		}
		return SUCCESS;
	}
	// 获取总用户文档数
	public String getAllUserDocCnt() {
		allUserDocInfo = userDocStatsService.getAllUserDocInfo();
		return SUCCESS;
	}
	
	public UserDocStatsService getUserDocStatsService() {
		return userDocStatsService;
	}
	
	
	public void setUserDocStatsService(UserDocStatsService userDocStatsService) {
		this.userDocStatsService = userDocStatsService;
	}
	
	
	public HashMap<String, Object> getMapResult() {
		return mapResult;
	}
	
	public void setMapResult(HashMap<String, Object> mapResult) {
		this.mapResult = mapResult;
	}
	
	public String getIsCount() {
		return isCount;
	}
	
	public void setIsCount(String isCount) {
		this.isCount = isCount;
	}
	
	public String getFindDocName() {
		return findDocName;
	}
	
	public void setFindDocName(String findDocName) {
		this.findDocName = findDocName;
	}
	
	public String getFindTmplType() {
		return findTmplType;
	}
	
	public void setFindTmplType(String findTmplType) {
		this.findTmplType = findTmplType;
	}
	
	public String getFindDateStart() {
		return findDateStart;
	}
	
	public String getFindDateEnd() {
		return findDateEnd;
	}
	
	public void setFindDateStart(String findDateStart) {
		this.findDateStart = findDateStart;
	}
	
	public void setFindDateEnd(String findDateEnd) {
		this.findDateEnd = findDateEnd;
	}
	public List<TypeInfoDto> getTypeComboList() {
		return typeComboList;
	}
	public void setTypeComboList(List<TypeInfoDto> typeComboList) {
		this.typeComboList = typeComboList;
	}
	public String getFindTmplTypetop() {
		return findTmplTypetop;
	}
	public void setFindTmplTypetop(String findTmplTypetop) {
		this.findTmplTypetop = findTmplTypetop;
	}

	public String getFindPubFlg() {
		return findPubFlg;
	}

	public void setFindPubFlg(String findPubFlg) {
		this.findPubFlg = findPubFlg;
	}

	public HashMap<String, Object> getUserDocMapResult() {
		return userDocMapResult;
	}

	public void setUserDocMapResult(HashMap<String, Object> userDocMapResult) {
		this.userDocMapResult = userDocMapResult;
	}

	public UserDocInfoDto getAllUserDocInfo() {
		return allUserDocInfo;
	}

	public void setAllUserDocInfo(UserDocInfoDto allUserDocInfo) {
		this.allUserDocInfo = allUserDocInfo;
	}

	public String getFindUserId() {
		return findUserId;
	}
	public void setFindUserId(String findUserId) {
		this.findUserId = findUserId;
	}

	public String getFindDesignerName() {
		return findDesignerName;
	}
	public void setFindDesignerName(String findDesignerName) {
		this.findDesignerName = findDesignerName;
	}
}
